package com.five.reader.controller;

import com.five.exception.BusinessException;
import com.five.pojo.Reader;
import com.five.reader.service.ReaderService;
import com.five.reader.vo.ReaderVO;
import com.five.result.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/reader")
@RequiredArgsConstructor
public class ReaderController {

    private final ReaderService readerService;

    /**
     * 根据ID获取读者信息
     * @param request 包含readerId的请求体
     * @return 读者信息
     */
    @PostMapping("/getById")
    public Result<ReaderVO> getReaderById(@RequestBody Map<String, Long> request) {
        try {
            Long readerId = request.get("readerId");
            if (readerId == null) {
                return Result.error("读者ID不能为空");
            }
            ReaderVO readerVO = readerService.getReaderInfo(readerId);
            return Result.success(readerVO);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("系统错误: " + e.getMessage());
        }
    }

    /**
     * 根据用户名获取读者信息
     * @param request 包含username的请求体
     * @return 读者信息
     */
    @PostMapping("/getByUsername")
    public Result<ReaderVO> getReaderByUsername(@RequestBody Map<String, String> request) {
        try {
            String username = request.get("username");
            if (username == null || username.trim().isEmpty()) {
                return Result.error("用户名不能为空");
            }
            ReaderVO readerVO = readerService.getReaderByUsername(username);
            return Result.success(readerVO);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("系统错误: " + e.getMessage());
        }
    }

    /**
     * 根据手机号获取读者信息
     * @param request 包含phone的请求体
     * @return 读者信息
     */
    @PostMapping("/getByPhone")
    public Result<ReaderVO> getReaderByPhone(@RequestBody Map<String, String> request) {
        try {
            String phone = request.get("phone");
            if (phone == null || phone.trim().isEmpty()) {
                return Result.error("手机号不能为空");
            }
            ReaderVO readerVO = readerService.getReaderByPhone(phone);
            return Result.success(readerVO);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("系统错误: " + e.getMessage());
        }
    }

    /**
     * 根据邮箱获取读者信息
     * @param request 包含email的请求体
     * @return 读者信息
     */
    @PostMapping("/getByEmail")
    public Result<ReaderVO> getReaderByEmail(@RequestBody Map<String, String> request) {
        try {
            String email = request.get("email");
            if (email == null || email.trim().isEmpty()) {
                return Result.error("邮箱不能为空");
            }
            ReaderVO readerVO = readerService.getReaderByEmail(email);
            return Result.success(readerVO);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("系统错误: " + e.getMessage());
        }
    }

    /**
     * 更新读者信息
     * @param reader 读者信息
     * @return 更新结果
     */
    @PostMapping("/update")
    public Result<Boolean> updateReader(@RequestBody Reader reader) {
        try {
            if (reader == null || reader.getId() == null) {
                return Result.error("读者信息不能为空");
            }
            boolean result = readerService.updateReaderInfo(reader);
            return Result.success(result ? "更新成功" : "更新失败");
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("系统错误: " + e.getMessage());
        }
    }

    /**
     * 更新读者最后登录时间
     * @param request 包含readerId的请求体
     * @return 更新结果
     */
    @PostMapping("/updateLastLoginTime")
    public Result<Boolean> updateLastLoginTime(@RequestBody Map<String, Long> request) {
        try {
            Long readerId = request.get("readerId");
            if (readerId == null) {
                return Result.error("读者ID不能为空");
            }
            boolean result = readerService.updateLastLoginTime(readerId);
            return Result.success(result ? "更新成功" : "更新失败");
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("系统错误: " + e.getMessage());
        }
    }

    /**
     * 更新读者密码
     * @param request 包含readerId、newPassword、salt的请求体
     * @return 更新结果
     */
    @PostMapping("/updatePassword")
    public Result<Boolean> updatePassword(@RequestBody Map<String, Object> request) {
        try {
            Long readerId = request.get("readerId") != null ? 
                Long.valueOf(request.get("readerId").toString()) : null;
            String newPassword = (String) request.get("newPassword");
            String salt = (String) request.get("salt");
            
            if (readerId == null) {
                return Result.error("读者ID不能为空");
            }
            if (newPassword == null || newPassword.trim().isEmpty()) {
                return Result.error("新密码不能为空");
            }
            if (salt == null || salt.trim().isEmpty()) {
                return Result.error("盐值不能为空");
            }
            
            boolean result = readerService.updatePassword(readerId, newPassword, salt);
            return Result.success(result ? "密码更新成功" : "密码更新失败");
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("系统错误: " + e.getMessage());
        }
    }

    /**
     * 更新读者状态
     * @param request 包含readerId、status的请求体
     * @return 更新结果
     */
    @PostMapping("/updateStatus")
    public Result<Boolean> updateStatus(@RequestBody Map<String, Object> request) {
        try {
            Long readerId = request.get("readerId") != null ? 
                Long.valueOf(request.get("readerId").toString()) : null;
            Integer status = request.get("status") != null ? 
                Integer.valueOf(request.get("status").toString()) : null;
            
            if (readerId == null) {
                return Result.error("读者ID不能为空");
            }
            if (status == null) {
                return Result.error("状态不能为空");
            }
            
            boolean result = readerService.updateStatus(readerId, status);
            return Result.success(result ? "状态更新成功" : "状态更新失败");
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("系统错误: " + e.getMessage());
        }
    }

    /**
     * 删除读者（逻辑删除）
     * @param request 包含readerId的请求体
     * @return 删除结果
     */
    @PostMapping("/delete")
    public Result<Boolean> deleteReader(@RequestBody Map<String, Long> request) {
        try {
            Long readerId = request.get("readerId");
            if (readerId == null) {
                return Result.error("读者ID不能为空");
            }
            boolean result = readerService.deleteReader(readerId);
            return Result.success(result ? "删除成功" : "删除失败");
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("系统错误: " + e.getMessage());
        }
    }

}